package com.changgou.order.controller;


import com.alibaba.fastjson.JSON;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.order.config.TokenDecode;


import com.changgou.order.pojo.OrderItem1;
import com.changgou.order.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/cart")
public class CartController {


    @Autowired
    private CartService cartService;

    @Autowired
    private TokenDecode tokenDecode;


    @GetMapping("/addCart")
    public Result addCart(@RequestParam String skuId, @RequestParam Integer num) {

        // 动态获取登录人
        String username1 = tokenDecode.getUserInfo().get("username");


//        String username = "itcast";
        cartService.addCart(skuId, num, username1);

        return new Result(true, StatusCode.OK, "加入购物车成功");

    }


    @GetMapping("/list")
    public Map list() {

//        String username = "itcast";
        String username = tokenDecode.getUserInfo().get("username");

        Map map = cartService.list(username);

        return map;
    }


    @PostMapping("/buylist")
    public Map buylist( @RequestBody List<OrderItem1> list) {

        Map map = cartService.BuyList(list);
        return map;
    }


    @RequestMapping(value="/buylist1", method=RequestMethod.POST)
    public String buylist1( @RequestBody List<OrderItem1> list) {

        Map map = cartService.BuyList(list);
        String s = JSON.toJSONString(map);
        return s;
    }
}